/*
 * @Author: your name
 * @Date: 2020-09-15 16:34:49
 * @LastEditTime: 2020-09-17 08:33:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense_demo\assets\scripts\common\audio_switch.ts
 */

import { SoundMgr } from "../sound_mgr"

const {ccclass, property} = cc._decorator;

@ccclass
export default class AudioSwitch extends cc.Component {

    @property(cc.Integer)
    public type = 0;            // 0表示music开关，1表示音效开关
    @property(cc.SpriteFrame)   // 关闭状态图片
    public off_spriteframe: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)   // 打开状态图片
    public on_spriteframe: cc.SpriteFrame = null;

    @property(cc.Sprite)
    public sp: cc.Sprite = null;

    onLoad() {}

    // 根据播放状态，切换图片
    start() {
        if (this.type === 0) {          // 背景音乐的开关
            if (SoundMgr.b_music_mute) {
                this.sp.spriteFrame = this.off_spriteframe;
            } else {
                this.sp.spriteFrame = this.on_spriteframe;
            }
        }
        else if (this.type === 1) {     // 音效开关
            if (SoundMgr.b_effect_mute) {
                this.sp.spriteFrame = this.off_spriteframe;
            } else {
                this.sp.spriteFrame = this.on_spriteframe;
            }
        }
    }

    // 按钮事件，切换状态
    public on_switch_click(): void {
        let b_mute: number = undefined;
        if (this.type === 0) {      // 背景音乐
            b_mute = (SoundMgr.b_music_mute) ? 0 : 1;
            SoundMgr.set_music_mute(b_mute);
        } else if (this.type === 1) {
            b_mute = (SoundMgr.b_effect_mute) ? 0 : 1;
            SoundMgr.set_effect_mute(b_mute);
        }

        if (b_mute) {   // 0为没有静音，1为静音 off图片
            this.sp.spriteFrame = this.off_spriteframe;
        } else {    // on图片
            this.sp.spriteFrame = this.on_spriteframe;
        }
    }

}
